/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.leadiv.cards;

import com.leadiv.cards.Rook.RookCard;
import com.leadiv.cards.Rook.RookPlayer;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author pborrego
 */
public class Trick {
    private int led;
    private Vector<RookCard> cards;
    private int points;
    private int winner;
    private String trump;
    private int playCount;

    public Trick(int led, String trump)
    {
        this.led = led;
        this.cards = new Vector<RookCard>();
        this.trump = trump;
        this.points = 0;
        this.winner = 0;
        this.playCount = 0;
    }
    
    public Trick(int led, RookCard card, String trump)
    {
        this.led = led;
        this.cards = new Vector<RookCard>();
        this.trump = trump;
        this.points = 0;
        this.winner = 0;
        this.playCount = 0;

        this.play(card);
    }

    public void play(RookCard card)
    {
        this.cards.add(card);
        this.points += card.getValue();
        if(this.trumps(card, this.cards.get(this.winner)))
        {
            this.winner = this.playCount;
        }
        this.playCount++;
    }

    public boolean trumps(RookCard c1, RookCard c2)
    {
        final int followsSuitMod = 10;
        final int trumpMod = 100;

        int fv1 = c1.getFace().equals("Rook") ? c1.getValue().intValue() : Integer.parseInt(c1.getFace());
        int fv2 = c2.getFace().equals("Rook") ? c2.getValue().intValue() : Integer.parseInt(c2.getFace());

        fv1 = isLedSuit(c1) ? fv1 * followsSuitMod : fv1;
        fv2 = isLedSuit(c2) ? fv2 * followsSuitMod : fv2;

        fv1 = isTrump(c1) ? fv1 * trumpMod : fv1;
        fv2 = isTrump(c2) ? fv2 * trumpMod : fv2;

        return fv1 > fv2;
    }

    public boolean isLedSuit(RookCard c)
    {
        String cardSuit = (c.getSuit().equals("Trump")) ? this.trump : c.getSuit();

        return this.getLedSuit().equals(cardSuit);
    }

    public String getLedSuit()
    {
        return this.cards.get(0).getSuit().equals("Trump") ?
            this.trump : this.cards.get(0).getSuit();
    }

    public RookPlayer getWinner(Vector<RookPlayer> players)
    {
        return players.get((this.led + this.winner) % players.size());
    }

    public int getLed()
    {
        return this.led;
    }

    public int getWinnerIndexOffset()
    {
        return this.winner;
    }

    public Vector<RookCard> getCards()
    {
        return this.cards;
    }

    public boolean isTrump(RookCard c)
    {
        if(c.getSuit().equals("Trump"))
            return true;

        return this.trump.equals(c.getSuit());
    }

    public boolean isEmptyTrick()
    {
        return this.cards.isEmpty();
    }

    public int getPoints() {
        return points;
    }

    public void printTrick()
    {
        Iterator<RookCard> iter = this.cards.iterator();

        System.out.println("'Led' Player index: " + this.led);
        while(iter.hasNext())
        {
            iter.next().printCard();
        }
        System.out.println("Points: " + this.points);
        System.out.println("Winner Player index: " + this.winner);
    }
}
